package com.sita.tboard.roadtrust.helper;

import android.widget.Toast;

import com.nostra13.universalimageloader.utils.L;
import com.sita.bike.persistence.RtNoteEntity;
import com.sita.bike.persistence.RtNoteEntityDao;
import com.sita.bike.rest.RestClient;
import com.sita.bike.rest.model.RtResourceNote;
import com.sita.bike.rest.model.request.RtLeaveNoteListRequest;
import com.sita.bike.rest.model.request.RtLeaveNoteNewRequest;
import com.sita.bike.rest.model.response.RtResourceNoteListResponse;
import com.sita.bike.rest.model.response.RtResourceNoteNewResponse;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.utils.AccountUtils;
import com.sita.tboard.roadtrust.event.RtNoteRefreshEvent;

import java.util.List;

import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiaodong on 16/3/28.
 */
public class RtNoteNetworkHelper {
    private static class RtNoteHelperHolder {
        private static RtNoteNetworkHelper INSTANCE = new RtNoteNetworkHelper();
    }

    private RtNoteNetworkHelper() {
    }

    public static RtNoteNetworkHelper getInstance() {
        return RtNoteHelperHolder.INSTANCE;
    }

    public void getNoteList(String resourceId, final Integer page){
        RtLeaveNoteListRequest request = new RtLeaveNoteListRequest();
        request.resId = resourceId;
        if (page != null) {
            request.pageNumber = String.valueOf(page);
            request.pageSize = String.valueOf(10);
        }
        RestClient.getRestNormalService().rtResourceListNote(request, new Callback<RtResourceNoteListResponse>() {
            @Override
            public void success(RtResourceNoteListResponse reply, Response response) {
                if (reply.errorCode.equals("0")) {
                    RtNoteEntityDao dao = GlobalContext.getDaoSession().getRtNoteEntityDao();
                    List<RtResourceNote> notes = reply.data.notes;
                    for (RtResourceNote note : notes) {
                        if (null == dao.load(Long.valueOf(note.noteId))) {
                            RtNoteEntity entity = new RtNoteEntity();
                            entity.setId(Long.valueOf(note.noteId));
                            entity.setAccountId(note.accountId);
                            entity.setResourceId(note.resId);
                            entity.setCreateTime(note.createTime);
                            entity.setMessage(note.message);
                            dao.insertOrReplace(entity);
                        }
                    }
                    if (page != null && page.intValue() == 0) {
                        EventBus.getDefault().post(new RtNoteRefreshEvent(RtNoteRefreshEvent.Mode.REFRESH));
                    } else {
                        EventBus.getDefault().post(new RtNoteRefreshEvent(RtNoteRefreshEvent.Mode.MORE));
                    }
                }
            }

            @Override
            public void failure(RetrofitError error) {
                L.e(error);
            }
        });
    }

    public void sendNote(String resourceId, String message){
        RtLeaveNoteNewRequest request = new RtLeaveNoteNewRequest();
        request.accountId = AccountUtils.getAccountID();
        request.resId = resourceId;
        request.message = message;
        RestClient.getRestNormalService().rtResourceNewNote(request,
                new Callback<RtResourceNoteNewResponse>() {
            @Override
            public void success(RtResourceNoteNewResponse reply, Response response) {
                if (reply.errorCode.equals("0")) {
                    RtNoteEntityDao dao = GlobalContext.getDaoSession().getRtNoteEntityDao();
                    List<RtResourceNote> notes = reply.data;
                    for (RtResourceNote note : notes) {
                        RtNoteEntity entity = new RtNoteEntity();
                        entity.setId(Long.valueOf(note.noteId));
                        entity.setAccountId(note.accountId);
                        entity.setResourceId(note.resId);
                        entity.setCreateTime(note.createTime);
                        entity.setMessage(note.message);
                        dao.insertOrReplace(entity);

                        EventBus.getDefault().post(new RtNoteRefreshEvent(RtNoteRefreshEvent.Mode.NEW_NOTE));
                    }
                }
            }

            @Override
            public void failure(RetrofitError error) {
                L.e(error);
                Toast.makeText(GlobalContext.getGlobalContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
